﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using QZone.crlclr.album.data;
using System.Net;

namespace QZone.crlclr.album
{
    public delegate void DownLoadFileProgress(string fileName);
    public class QQZoneManager
    {
        public const string Albums3Templet = "http://xap.photo.qq.com/{0}/16";
        public const string Albums4Templet = "http://zxp.photo.qq.com/{0}/16";

        public const string Photos3Templet = "http://xastatic.photo.qq.com/http_staload.cgi?{0}/{1}";
        public const string Photos4Templet = "http://static.photo.qq.com/http_staload.cgi?{0}/{1}";

        private string rootPath;
        private string qqcode;

        private static int zoneType = 3;


        /// <summary>
        ///  是否为新版的QQ空间;
        /// </summary>
        public static bool isNews{
            set
            {
                zoneType = value ? 4 : 3;
            }
        }

        public event DownLoadFileProgress onDownLoadFileProgress;
        private QQZoneManager()
        {
        }


        private static QQZoneManager instance=null;
        public static QQZoneManager getInstance()
        {
            if (instance == null)
            {
                instance = new QQZoneManager();
            }
            return instance;
        }

        /// <summary>
        ///  下载的QQ号码及要保存的路径;
        /// </summary>
        /// <param name="qqcode"></param>
        /// <param name="rootPath"></param>
        public void download(string qqcode, string rootPath){
            this.qqcode = qqcode;
            this.rootPath = rootPath;


            if (Directory.Exists(rootPath) == false)
            {
                Directory.CreateDirectory(rootPath);
            }

            AlbumsXMLLoader loader = new AlbumsXMLLoader();
            loader.loadQQ(this.qqcode);

            string cfg=Path.Combine(rootPath,"config.xml");
            loader.getConfig().Save(cfg);
            this.onDownLoadFileProgress(cfg);


            List<Album> albums=loader.albums;

            if (albums.Count == 0)
            {
                //加载不到相册;
            }

            foreach (Album item in albums)
            {
                this.downloadAlbum(item);
            }

        }


        /// <summary>
        ///  下载单本相册内所有文件;
        /// </summary>
        /// <param name="album"></param>
        public void downloadAlbum(Album album)
        {
            string albumPath=Path.Combine(rootPath,album.key);
            if (Directory.Exists(albumPath)==false)
            {
                Directory.CreateDirectory(albumPath);
            }

            string coverPath=Path.Combine(albumPath,"thumb.jpg");
            downloadFile(album.thumbPath, coverPath);


            PhotosXMLLoader loader = new PhotosXMLLoader();
            loader.loadAlbum(qqcode, album.key);
            string cfg = Path.Combine(albumPath, "config.xml");
            loader.getConfig().Save(cfg);
            this.onDownLoadFileProgress(cfg);


            List<Picture> list = loader.photos;

            string thumbPath = Path.Combine(albumPath, "thumb");
            string urlPath = Path.Combine(albumPath, "ipage");

            if (Directory.Exists(thumbPath) == false) Directory.CreateDirectory(thumbPath);
            if (Directory.Exists(urlPath) == false) Directory.CreateDirectory(urlPath);

            string tempPath;
            foreach (Picture item in list)
            {
                tempPath = Path.Combine(thumbPath, item.name+".jpg");
                if(File.Exists(tempPath)==false)downloadFile(item.thumbPath, tempPath);

                tempPath = Path.Combine(urlPath, item.name+".jpg");
                if(File.Exists(tempPath)==false)downloadFile(item.urlPath, tempPath);
            }
        }

        /// <summary>
        /// 下载单个文件,保存路径;
        /// </summary>
        /// <param name="url"></param>
        /// <param name="savePath"></param>
        public void downloadFile(string url, string savePath)
        {
            WebClient client = new WebClient();
            try
            {
                 client.DownloadFile(url, savePath);
                 this.onDownLoadFileProgress(url);
            }
            catch (Exception e)
            {
                string msg = e.Message;
                //加载文件错误;
            }
        }


        #region

        /// <summary>
        ///  取得模板路径;
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string getTempletString(string type)
        {
            string temp = string.Empty;
            switch (type)
            {
                case "albums" :
                    temp = zoneType == 3 ? Albums3Templet : Albums4Templet;
                    break;
 
                case "photos" :
                    temp = zoneType == 3 ? Photos3Templet : Photos4Templet;
                    break;
            }

            return temp;
        }


        public static string getAlbumTemplet()
        {
            return getTempletString("albums");
        }

        internal static string getPhotosTemplet()
        {
            return getTempletString("photos");
        }

        #endregion
    }
}
